package com.jz.community.basecomm.loaction;

import android.content.Context;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;

import java.util.List;

public class PoiManager {

    private PoiSearch.Query query;
    private PoiSearch poiSearch;
    private static final String SEARCH_CONTENT =
            "汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|" +
                    "医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|" +
                    "金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施";


    public PoiManager(Context context, double lat, double lon, String keyWord, boolean isKeyWord, IPoiSearchResult iPoiSearchResult,String cityCode) {
        query = new PoiSearch.Query(keyWord, SEARCH_CONTENT, cityCode);
        // 设置每页最多返回多少条poiItem
        query.setPageSize(10);
        //设置查询页码
        query.setPageNum(0);
        poiSearch = new PoiSearch(context, query);
        poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
            @Override
            public void onPoiSearched(PoiResult poiResult, int i) {
                if (i == 1000) {
                    //成功
                    if (null != poiResult && null != poiResult.getPois() && poiResult.getPois().size() > 0) {
                        List<PoiItem> list = poiResult.getPois();
                        iPoiSearchResult.poiSearchResult(list);
                    } else {
                        iPoiSearchResult.poiSearchResult(null);
                    }
                } else {
                    iPoiSearchResult.poiSearchResult(null);
                }
            }

            @Override
            public void onPoiItemSearched(PoiItem poiItem, int i) {
            }
        });
        if (isKeyWord) {
            LatLonPoint point = new LatLonPoint(lat, lon);
            poiSearch.setBound(new PoiSearch.SearchBound(point, 2000, true));
        }
        poiSearch.searchPOIAsyn();
    }


    public interface IPoiSearchResult {
        void poiSearchResult(List<PoiItem> list);
    }
}
